/**
 * Created by Administrator on 2018/11/7 0007.
 */
let Promise = require('bluebird');
const utils = require('../../public/utils');
const bookSortModel = require('../../model/bookSort-model');
const bookModel = require('../../model/book-model');

const obj = {
    /*
    * 根据条件判断一级/二级分类中是否已存在该数据
    * */
    sortIsExistence(paramsObj) {
        return new Promise((resolve,reject) => {
            bookSortModel.findOne(paramsObj,(err,data) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                    return;
                }
                if(data) {
                    resolve(true);
                }else {
                    resolve(false);
                }
            })
        })
    },
    /*
    * 根据parentId获取父级分类名
    * */
    getParentNameByParentId(parentId) {
        return new Promise((resolve,reject) => {
            let parentName;
            if(parentId === '*') {
                parentName = '一级分类';
                resolve(parentName);
            }else {
                bookSortModel.findOne({_id: parentId},(err,data) => {
                    console.log(data);
                    if(err || !data) {
                        resolve(false);
                    }else {
                        parentName = data.name;
                        resolve(parentName);
                    }
                })
            }

        })
    },

    /*
    * 增加分类
    * */
     addSortData(dataObj) {
        return new Promise(async (resolve,reject) => {
            const parentId = dataObj.parentId;
            let parentName = await this.getParentNameByParentId(parentId);
            if(!parentName) {
                resolve(9999);
                return;
            }
            const book = new bookSortModel({...dataObj,parentName});
            book.save((err) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                    return;
                }
                resolve(true)
            })

        })
    },

    /*
    * 删除分类
    * */
    deleteSortData(dataObj) {
        return new Promise((resolve,reject) => {
            bookSortModel.remove(dataObj,(err,data) => {
                if(err) {
                    resolve(9999);
                    return;
                }
                resolve(true)
            })
        })
    },

    /*
    * 更新分类
    * */
    upDateSortData(_id,upDateObj) {
        return new Promise(async (resolve,reject) => {
            let parentName = await this.getParentNameByParentId(upDateObj.parentId);
            if(!parentName) {
                resolve(9999);
                return;
            }
            bookSortModel.update({_id},{...upDateObj,parentName},(err,data) => {
                if(err) {
                    resolve(9999);
                    return;
                }
                resolve(true);
            })
        })
    },

    /*
    * 查找所有分类
    * */
    findSortData(findObj={}) {
        return new Promise((resolve,reject) => {
            bookSortModel.find(findObj,(err,docs) => {
                if(err) {
                    resolve(9999);
                    return;
                }
                let list = JSON.parse(JSON.stringify(docs));
                list.forEach(item => {
                    item.createTime = utils.dateFormat(item.createTime,'YYYY-MM-DD HH:mm');
                    item.upDateTime = utils.dateFormat(item.upDateTime,'YYYY-MM-DD HH:mm');

                })
                resolve(list);
            })
        })
    },


    /*
    * 添加书
    * */
    addBook(dataObj) {
        return new Promise((resolve,reject) => {
            const book = new bookModel(dataObj);
            book.save((err) => {
                if(err) {
                    console.log(err);
                    resolve(false);
                    return;
                }
                resolve(true)
            })
        })
    },

    /*
    * 删除书
    * */
    deleteBook(delParams) {
        return new Promise((resolve,reject) => {
           bookModel.remove(delParams,(err,data) => {
                if(err) {
                    resolve(false);
                    return;
                }
                resolve(true)
            })
        })
    },

    /*
    * 修改书
    * */
    updateBook(_id,upDateData) {
        return new Promise((resolve,reject) => {
            bookModel.update({_id},upDateData,(err,data) => {
                if(err) {
                    console.log(err);
                    resolve(false);
                    return;
                }
                resolve(true)
            })
        })
    },

    /*
    * 获取书
    * */
    findBook(findParam,ignoreObj={},handleObj={}) {
        return new Promise((resolve,reject) => {
            bookModel.find(findParam,ignoreObj,handleObj,(err,docs) => {
                if(err) {
                    console.log(err);
                    resolve(9999);
                    return;
                }
                let list = JSON.parse(JSON.stringify(docs));
                list.forEach(item => {
                    item.createTime = utils.dateFormat(item.createTime),
                        item.upDateTime = utils.dateFormat(item.upDateTime)

                })
                resolve(list);
            })
        })
    },
    /*
    * 获取书集合总条数
    *
    * */
    bookListCount() {
        return new Promise((resolve,reject) => {
            bookModel.getCount(total => {
                resolve(total);
            });
        })

    },
}
module.exports = obj;
